/**
 * 
 */
package neptune.web.client.controller;

import neptune.web.client.services.experiment.ExperimentService;
import neptune.web.client.services.experiment.ExperimentServiceAsync;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.ExperimentSummaryView;
import neptune.web.client.view.OperativeView;
import neptune.web.client.view.SuperView;
import neptune.web.client.view.ViewManager;
import neptune.web.client.view.components.dialogs.PropertiesDialog;
import neptune.web.client.view.components.dialogs.PropertiesDialogListener;

import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author Roberto Bifulco [info@robertobifulco.it, robertobifulco.it]
 * 
 */
public class ExperimentCreationDialogController implements
		PropertiesDialogListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see neptune.web.client.view.components.dialogs.PropertiesDialogListener#okPressed(java.lang.String[][],
	 *      neptune.web.client.view.components.dialogs.PropertiesDialog)
	 */
	public void okPressed(String[][] properties, final PropertiesDialog sender) {
		final String experimentName = properties[0][1];

		ExperimentServiceAsync service = ExperimentService.Util.getInstance();

		service.createExperiment(experimentName, new AsyncCallback<Boolean>() {

			public void onSuccess(Boolean arg0) {
				String message = "Experiment " + experimentName
						+ " successfully created!";
				sender.showMessage(message);
				ConsoleManager.addConsoleMessage(message, false);

				SuperView operative = (SuperView) ViewManager.getViewManager()
						.getViewByName(OperativeView.DEFAULT_TOKEN);
				ExperimentSummaryView expView = (ExperimentSummaryView) operative
						.getViewByName(ExperimentSummaryView.DEFAULT_TOKEN);
				expView.reset();
				expView.update();

				// TODO Aggiungere il cambio sulla ExperimentView di interesse
			}

			public void onFailure(Throwable arg0) {
				String message = "Experiment '" + experimentName
						+ "' creation failed, cause: " + arg0.getMessage();
				ConsoleManager.addConsoleMessage(message, true);
				sender.showMessage(message);
			}

		});
	}

}
